//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    account:'',
    pwd:'',
    pwdvisible:false,
    hidden:true,
    title:'',
    Loading:false,
    pwdfocus:false
  },
  confirm(){
    this.setData({
      hidden:true
    })
  },
  lookpwd(){
    this.setData({
      pwdvisible:!this.data.pwdvisible,
      pwdfocus:true
    })
  },
  accountinput(e){
    this.setData({
      account:e.detail.value
    })
  },
  pwdtinput(e){
    this.setData({
      pwd:e.detail.value
    })
  },
  login (){
    let self = this;
    if(self.data.account.length==0||self.data.pwd==0){
      self.setData({
        title:"请输入账号和密码",
        hidden:false
      })
      return;
    }
    self.setData({Loading:true})
        getApp().request({
          url:'login',
          data:{
            mobile:self.data.account,
            psw: self.data.pwd
          },success:function(res) {
            console.log(res);
            if(res.data.flag){
              getApp().globalData.UserData = res.data.data;
              wx.setStorageSync('localaccount', self.data.account)
              wx.setStorageSync('localpsw', self.data.pwd)
              wx.setStorageSync('localname', res.data.data.nick_name)
              wx.setStorageSync('localicon', res.data.data.icon)
              self.setData({Loading:false})
              self.loginsuc();
            }
            else{
              self.setData({
                title:res.data.msg,
                hidden:false,
                Loading:false
              })
            }
          },fail(){
            self.setData({
                title:"请求失败",
                hidden:false,
                Loading:false
            })
          }
        },true)
  },
  loginsuc(){
    try {
      wx.reLaunch({
        url: '../realtime/realtime'
      })
    } catch (e) {
      wx.redirectTo({
        url: '../realtime/realtime'
      })
    }
  }
})
